var appI =getApp()
var bgMusic = appI.globalData.bgMusic
var globalData = appI.globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
    gezi:'',
    title:'',
    gepuIconShow:0,
    gepuUrl:'',
    nowMusicId:{},
    playListStatus:0,
    progressVal:'0px',
    progress_nowTime:'00:00:00',
    progress_LoateTime:'00:00:00',
    beisu:0,
    bodanList:{},
    lishiJiLu:{},
    myBodan:{},
    ani:'',
    activeSwiperChange_list:2,
    zhezhaoHidden:'0'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var self =  this;

    globalData.lishiMusic()

    this.setGepuIconShow()

    if(wx.getStorageSync('nowMusicId')!==''){

      this.setData({nowMusicId:JSON.parse(wx.getStorageSync('nowMusicId'))})
    }

    // 设置播放链接
    globalData.playURLIsOrNo()

    
  },
  //设置歌谱的图标是否显示
  setGepuIconShow:function(){

    var tabId = JSON.parse(wx.getStorageSync('nowMusicId')).tabid

    if(tabId == 1){
      this.setData({
        gepuIconShow:1,
       
      })
    }
  },
    //显示歌谱
  gepuHidde:function(){

    globalData.gepuHidde()

  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    // console.log('当前页面被展示 ')

    var self =  this;

    var nowMusicId = wx.getStorageSync('nowMusicId')

     /*设置歌词和标题*/
      this.geziTitleSet()


      globalData.onPlay(function(){


        var nowMusicId = wx.getStorageSync('nowMusicId')

        var nowObj = JSON.parse(nowMusicId)

        self.setData({
          nowMusicId:nowObj
        })

        /*更新歌词和标题*/
        self.geziTitleSet()


      })

      globalData.onPause(function(){

        var nowMusicId = wx.getStorageSync('nowMusicId')

        var nowObj = JSON.parse(nowMusicId)

        self.setData({
          nowMusicId:nowObj
        })

        /*更新歌词和标题*/
        self.geziTitleSet()


      })


      //进度条设置 
      this.setProgress();

      //获取当前的动态播放列表数据
      var dongTaiLieBiao = globalData.getDongTaiLieBiao()

      this.setData({
        bodanList:dongTaiLieBiao.bodanList,
        lishiJiLu:dongTaiLieBiao.lishiJiLu,
        myBodan:dongTaiLieBiao.myBodan,
        playListStatus:wx.getStorageSync('playListStatus'),
      })


  },
  /*设置歌词和标题*/
  geziTitleSet:function(){

    var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'))

    var listInfo = JSON.parse(wx.getStorageSync(nowMusicId.uid)).programs

    var tabid = nowMusicId.tabid

    switch(tabid){
      case 0:

        for(var i = 0 ; i < listInfo.length ; i++){

          if(listInfo[i].id == nowMusicId.id){
    
            var gezi = listInfo[i].description
    
            break;
          }
    
        }
      
      break;
      case 1:

        var gezi = '暂无歌词'

      break;
      case 2:

        var gezi =''
        
        for(var i = 0 ; i < listInfo.length ; i++){

          if(listInfo[i].id == nowMusicId.id){
    
            var bs = listInfo[i].banshu

            for(var j =0 ; j < bs.length ; j++){

              if(bs[j].huayu !== ''){

                gezi += bs[j].huayu +'\n'
              }


            }
    
            break;
          }
    
        }

      break;
    }

    this.setData({
      title:nowMusicId.name,
      gezi:gezi,
    })


  }
  ,
// 显示动态列表

  showListBoDan:function(){


    var animation = globalData.showListBoDan('show').export()

    this.setData({
     ani: animation,
     zhezhaoHidden:'',
     playListStatus:wx.getStorageSync('playListStatus')
    })

    var dongTaiLieBiao = globalData.getDongTaiLieBiao()

    var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'))

     this.setData({
       bodanList:dongTaiLieBiao.bodanList,
       lishiJiLu:dongTaiLieBiao.lishiJiLu,
       myBodan:dongTaiLieBiao.myBodan,
       nowMusicId:nowMusicId,
     })

   
 },
// 隐藏动态列表
  hiddenBoDan:function(){

    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
      delay: 0
     });
     animation.top('100%').step()
     this.setData({
      ani: animation.export(),
      zhezhaoHidden:'1'
     })

  },
  swiperChange_list:function(e){

    var current = e.detail.current

    // console.log(current)

    this.setData({
      activeSwiperChange_list:current
    })

  },
  //通过动态列表播放音乐
  DongTaiPlay:function(e){


    var dongTaiLieBiao = globalData.DongTaiPlay(e)

     this.setData(dongTaiLieBiao)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  /*返回上一页面 */
  tonext:function(){

    wx.navigateBack({
      delta: 0,
    })

  },
  /*返回首页 */
  tohome:function(){

    wx.navigateBack({

      delta:5,
      
    })
  },

  // 底部播放按钮控制
  botPlayIconSet:function(e){

    var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'))

    var self =  this

    if(nowMusicId.status == 1){

      nowMusicId['status'] = 0
      bgMusic.pause()

    }else{

      nowMusicId['status'] = 1

      // 设置播放链接
      globalData.playURLIsOrNo()
 
    }

    this.setData({
      nowMusicId:nowMusicId,
    })

    wx.setStorage({
      data: JSON.stringify(nowMusicId),
      key: 'nowMusicId',
    })

  },
  
  //设置当前列表播放状态按钮
  onPlayListStatus:function(){

    
    var playListStatus = globalData.onPlayListStatus()
    this.setData({playListStatus:playListStatus})

  },
  //左右切换曲目
  lieBiaoXunHuan:function(e) {
      globalData.lieBiaoXunHuan(e)
  },
  
  //获取当前播放歌曲的开始时间与结事时间并设置进度条
  setProgress:function(){

    var self =  this;

    bgMusic.onTimeUpdate(function(){

      var duration = bgMusic.duration


      var currentTime = bgMusic.currentTime


      var pre = Math.floor((currentTime / duration) * 100) //进度百分比

      self.progressWidth(function(w){
 

        var progressVal = (w * (pre / 100)) + 'px'




        self.setData({

          progressVal:progressVal
        })

      })


      self.setData(
        {
          progress_nowTime:globalData.dateForma(currentTime),
          progress_LoateTime:globalData.dateForma(duration),
        }
      )

    })

  },
  //获取进度条的长度
  progressWidth:function(fun) {

    const query = wx.createSelectorQuery()
    query.select('.progress_item').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function(res){

      if(res){

        if(res[0] !== null){

          var W = res[0].width;

          fun(W)

        }

      }

    })

  },
  //设置点击进度条跳转到当前进度的功能
  progressSkip:function(e){

      //globalData.progressSkip(e)
    //console.log(e)

      var self =  this;

      this.progressWidth(function(w){

      var x =e.detail.x - e.currentTarget.offsetLeft 

      var pre = Math.floor(x / w *100)

      console.log('pre=>',pre)

      var duration = bgMusic.duration

      bgMusic.seek(duration * (pre / 100))


      self.setData({
        progressVal:x + 'px'
      })


    })


  },
  //设置倍速播放 微信暂时不支持
  onKuaiJin:function(){

    var beisu = this.data.beisu

    // console.log(beisu)
    beisu = beisu + 1
    if(beisu > 3){
      beisu = 0
    }

    // console.log(beisu)

    switch(beisu){

      case 0:
        bgMusic.playbackRate = 1
      break;
      case 1:
        bgMusic.playbackRate = 1.3
      break;
      case 2:
        bgMusic.playbackRate = 1.6
      break;
      case 3:
        bgMusic.playbackRate = 2
      break;
    }

    this.setData({
      beisu:beisu
    })


  },
  //下载音频 太费CDN流量暂时不开
  onDownLoadFile:function(){

    var self =  this

    globalData.getNowMusicInfo(function(res){

      var url = res.musicUrl

      // console.log(url)

      wx.downloadFile({
        url: url, //仅为示例，并非真实的资源
        success (res) {
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
          if (res.statusCode === 200) {
            wx.playVoice({
              filePath: res.tempFilePath
            })
          }
        }
      })


    })

    


  },
})